from redis import *
def str_crud():
    # string类型
    sr = StrictRedis(db=0,decode_responses=True)

    # 添加
    try:
        while True:
            key = input('请输入要添加的键:')
            values = input('请输入要添加的值:')
            if key in sr.keys():
                print('当前键已存在，请重新输入')
                continue
            else:
                result = sr.set(key,values)
                print(f'{result},添加成功')
                break
    except Exception as e:
        print(e)

    # 查询
    try:
        a = sr.keys()
        print(f'库中的键有：{a}')
        key = input('请输入要查询的键:')
        result = sr.get(key)
        print(f'{key} = {result}')
    except Exception as e:
        print(e)

    # 修改
    try:
        a = sr.keys()
        print(f'库中的键有：{a}')
        key = input('请输入要修改的键:')
        values = input('请输入要修改成的值:')
        result = sr.set(key,values)
        print(f'{result},修改成功')
    except Exception as e:
        print(e)

    # 删除
    try:
        a = sr.keys()
        print(f'库中的键有：{a}')
        key = input('请输入要删除的键:')
        result = sr.delete(key)
        print(f'{result},删除成功')
    except Exception as e:
        print(e)

def hash_crud():
    sr = StrictRedis(db=1,decode_responses=True)

    # 添加
    try:
        key = input('请输入要添加的键:')
        while True:
            property = input('请输入要添加的属性:')
            values = input('请输入要添加的属性值值:')
            if property in sr.hkeys(key):
                print('当前属性已存在，请重新输入')
                continue
            else:
                result = sr.hset(key,property,values)
                print(f'{result},添加成功')
            num = input('是否继续添加属性(1.是 0.否)')
            if num == '1':
                continue
            else:
                break
    except Exception as e:
        print(e)

    # 查询
    try:
        a = sr.keys()
        print(f'库中的键有：{a}')
        key = input('请输入要查询的键:')
        result = sr.hkeys(key)
        print(f'{key}中属性有{result}')
        property = input('请输入要查询的属性:')
        result = sr.hget(key,property)
        print(f'{property} = {result}')
    except Exception as e:
        print(e)

    # 修改
    try:
        a = sr.keys()
        print(f'库中的键有：{a}')
        key = input('请输入要修改的键:')
        result = sr.hkeys(key)
        print(f'{key}中属性有{result}')
        while True:
            property = input('请输入要修改的属性:')
            values = input('请输入要修改成的属性值:')
            result = sr.hset(key,property,values)
            print(f'{result},修改成功')
            num = input('是否继续添加属性(1.是 0.否)')
            if num == '1':
                continue
            else:
                break
    except Exception as e:
        print(e)

    # 删除
    try:
        while True:
            a = sr.keys()
            print(f'库中的键有：{a}')
            num = input('是否删除键(1.是 0.否)')
            if num == '1':
                key = input('请输入要删除的键:')
                result = sr.delete(key)
                print(f'{result},删除成功')
                continue
            else:
                key = input('请输入要删除属性的键:')
                property = input('请输入要删除的属性:')
                result = sr.hdel(key,property)
                print(f'{result},删除成功')
                break
    except Exception as e:
        print(e)

def list_crud():
    # string类型
    sr = StrictRedis(db=2,decode_responses=True)

    # 添加
    try:
        key = input('请输入要添加的键:')
        while True:
            num = int(input('请输入要添加的值的个数:'))
            for i in range(num):
                values = input(f'请输入第{i+1}个要添加的值:')
                result = sr.rpush(key,values)
            print(f'{result},添加成功')
            num = input('是否继续添加数据(1.是 0.否)')
            if num == '1':
                continue
            else:
                break
    except Exception as e:
        print(e)

    # 查询
    try:
        a = sr.keys()
        print(f'库中的键有：{a}')
        key = input('请输入要查询的键:')
        result = sr.lrange(key,0,-1)
        print(f'{key}中的数据有{result}')
    except Exception as e:
        print(e)

    # 修改
    try:
        a = sr.keys()
        print(f'库中的键有：{a}')
        key = input('请输入要修改的键:')
        result = sr.lrange(key, 0, -1)
        print(f'该列表的共{len(result)}个数据')
        num = int(input('请输入要修改第几个数据:'))
        values = input('请输入要修改成的值:')
        result = sr.lset(key,num-1,values)
        print(f'{result},修改成功')
    except Exception as e:
        print(e)

    # 删除
    try:
        while True:
            a = sr.keys()
            print(f'库中的键有：{a}')
            num = input('是否删除键(1.是 0.否)')
            if num == '1':
                key = input('请输入要删除的键:')
                result = sr.delete(key)
                print(f'{result},删除成功')
                continue
            else:
                key = input('请输入要删除数据的键:')
                values = input('请输入要删除的数据:')
                result = sr.lrem(key,0,values)
                print(f'{result},删除成功')
                break
    except Exception as e:
        print(e)

def set_crud():
    # string类型
    sr = StrictRedis(db=3,decode_responses=True)

    # 添加
    try:
        set1 = set()
        key = input('请输入要添加的键:')
        while True:
            num = int(input('请输入要添加的值的个数:'))
            for i in range(num):
                values = input(f'请输入第{i+1}个要添加的值:')
                set1.add(values)
            print(set1)
            for i in set1:
                result = sr.sadd(key,i)
            print('添加成功')
            num = input('是否继续添加数据(1.是 0.否)')
            if num == '1':
                continue
            else:
                break
    except Exception as e:
        print(e)

    # 查询
    try:
        a = sr.keys()
        print(f'库中的键有：{a}')
        key = input('请输入要查询的键:')
        result = sr.smembers(key)
        print(f'{key}中的数据有{result}')
    except Exception as e:
        print(e)

    # 删除
    try:
        while True:
            a = sr.keys()
            print(f'库中的键有：{a}')
            num = input('是否删除键(1.是 0.否)')
            if num == '1':
                key = input('请输入要删除的键:')
                result = sr.delete(key)
                print(f'{result},删除成功')
                continue
            else:
                key = input('请输入要删除数据的键:')
                values = input('请输入要删除的数据:')
                result = sr.srem(key,values)
                print(f'{result},删除成功')
                break
    except Exception as e:
        print(e)

def zset_crud():
    # string类型
    sr = StrictRedis(db=4,decode_responses=True)

    # 添加
    try:
        set1 = set()
        key = input('请输入要添加的键:')
        while True:
            num = int(input('请输入要添加的值的个数:'))
            for i in range(num):
                values = input(f'请输入第{i+1}个要添加的值:')
                set1.add(values)
            num = 1
            for i in set1:
                score = int(input(f'请输入第{num}个数据的权重:'))
                mapping = {i:score}
                result = sr.zadd(key,mapping)
                num += 1
            print('添加成功')
            num = input('是否继续添加数据(1.是 0.否)')
            if num == '1':
                continue
            else:
                break
    except Exception as e:
        print(e)

    # 查询
    try:
        a = sr.keys()
        print(f'库中的键有：{a}')
        key = input('请输入要查询的键:')
        result = sr.zrange(key,0,-1)
        print(f'{key}中的数据有{result}')
    except Exception as e:
        print(e)

    # 删除
    try:
        while True:
            a = sr.keys()
            print(f'库中的键有：{a}')
            num = input('是否删除键(1.是 0.否)')
            if num == '1':
                key = input('请输入要删除的键:')
                result = sr.delete(key)
                print(f'{result},删除成功')
                continue
            else:
                key = input('请输入要删除数据的键:')
                values = input('请输入要删除的数据:')
                result = sr.zrem(key,values)
                print(f'{result},删除成功')
                break
    except Exception as e:
        print(e)

def menu():
    while True:
        print('- '* 15)
        print('1.string')
        print('2.hash')
        print('3.list')
        print('4.set')
        print('5.zset')
        print('按其他键退出')
        num = input('请选择要操作的数据类型:')
        if num == '1':
            str_crud()
        elif num == '2':
            hash_crud()
        elif num == '3':
            list_crud()
        elif num == '4':
            set_crud()
        elif num == '5':
            zset_crud()
        else:
            break

if __name__ == '__main__':
    menu()